package mq.controller;

import com.smotech.newbus.basics.mq.service.IMqMessageAbnormalService;
import com.smotech.newbus.basics.mq.service.IMqMessageCumulateService;
import com.smotech.newbus.common.enums.BizError;
import com.smotech.newbus.common.log.annotations.Log;
import com.smotech.newbus.common.utils.result.Response;
import com.smotech.newbus.settlement.bill.common.notice.dto.DeductionNoticeDto;
import com.smotech.newbus.settlement.common.utils.MqDeductionNoticeUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;

/**
 * @description: MQ手动接口
 * @author: zk
 * @since: 2022-08-16 13:38:53
 */
@Slf4j
@RestController
@RequestMapping("/mq")
@Api(value = "MQSendController", tags = "MQ手动接口")
public class MQSendController {
    @Autowired
    private IMqMessageAbnormalService mqMessageAbnormalService;
    @Autowired
    private IMqMessageCumulateService mqMessageCumulateService;
    @Resource
    private MqDeductionNoticeUtil mqDeductionNoticeUtil;

    @PostMapping("/resend")
    @ApiOperation("MQ异常消息重新发送")
    @Log(prefix = "MQ异常消息重新发送")
    public Response resendMessage(@ApiParam(value = "类型 : 1-到交换机异常，2-到队列异常") @RequestParam Integer type) {
        mqMessageAbnormalService.resendMessage(type);
        return Response.ok();
    }

    @PostMapping("/cumulate-handle")
    @ApiOperation("扣款结果通知堆积消息补偿处理")
    @Log(prefix = "扣款结果通知堆积消息补偿处理")
    public Response cumulateMessageHandle(@ApiParam(value = "业务编号: 001-扣款结果通知") @RequestParam String busiNo) {
        mqMessageCumulateService.cumulateMessageHandle(busiNo);
        return Response.ok();
    }

    @PostMapping("deduction-notice-send")
    @ApiOperation("向mq发送扣款后消息")
    @Log(prefix = "向mq发送扣款后消息")
    public Response mqSend(@RequestBody @Validated DeductionNoticeDto noticeDto) {
        try {
            mqDeductionNoticeUtil.send(noticeDto);
            return Response.ok();
        } catch (Exception e) {
            log.error("向mq发送扣款后消息失败", e);
            return Response.failed(BizError.PARAM_ERROR, "向mq发送扣款后消息错误");
        }
    }


}
